/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control.game;

import aeronef.model.Ship;
import aeronef.model.Turret;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.Savable;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;
import java.io.IOException;

/**
 *
 * @author Hagan
 */
public class TurretTargetingControl extends AbstractControl implements Savable, Cloneable {

    private Ship ship;
    private Turret turret;
    
    public TurretTargetingControl(Turret turret, Ship ship) {
        this.ship = ship;
        this.turret = turret;
    }
    
    @Override
    protected void controlUpdate(float tpf) {
        
        Float turretTraverseRate = turret.getTraverseRate();
        Float turretTraversePos = turret.getTraversePos();
        Float turretTraverseArc = turret.getTraverseArc();
        Float shipTargeterTraversePos = ship.getOrderTarget();
        
        if (Math.abs(turretTraversePos) < turretTraverseArc) {
            if (turretTraversePos < shipTargeterTraversePos) {
                    turretTraversePos += ((turretTraverseRate * tpf));
            } else if (turretTraversePos > shipTargeterTraversePos) {
                    turretTraversePos -= ((turretTraverseRate * tpf));
            }
        }

        turret.setTraversePos(turretTraversePos);
        turret.setLocalRotation(new Quaternion().fromAngles(0, turretTraversePos*FastMath.DEG_TO_RAD, 0));
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void render(RenderManager rm, ViewPort vp) {
        // optional for advanced users, e.g. to display a debug shape
    }
    
    @Override
    public Control cloneForSpatial(Spatial spatial) { 
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    @Override
    public void read(JmeImporter im) throws IOException {
        super.read(im);
        // im.getCapsule(this).read(...);
    }

    @Override
    public void write(JmeExporter ex) throws IOException {
        super.write(ex);
        // ex.getCapsule(this).write(...);
    }
}
